package com.zbkj.service.service.product.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zbkj.common.enums.ProductTypeEnum;
import com.zbkj.common.model.product.Product;
import com.zbkj.common.vo.product.ProductQueryVo;
import com.zbkj.service.dao.product.ProductMapper;
import com.zbkj.service.service.product.ProductService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商品信息表 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-09-12
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Override
    public Product getByProductId(Long id) {
        if(Objects.isNull(id)) {
            return null;
        }
        return this.getByProductId(id,null);
    }

    @Override
    public Product getByProductId(Long id, List<Integer> statusList) {
        if(Objects.isNull(id)) {
            return null;
        }
        List<Product> plist = this.getByProductIds(Lists.newArrayList(id), statusList);
        return CollectionUtils.isEmpty(plist) ? null : plist.get(0);
    }

    @Override
    public List<Product> getByProductIds(List<Long> idList, List<Integer> statusList) {
        if(CollectionUtils.isEmpty(idList)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getDeleteFlag, 0);
        wrapper.in(Product::getId, idList);
        if(CollectionUtils.isNotEmpty(statusList)) {
            wrapper.in(Product::getProductStatus, statusList);
        }
       return this.list(wrapper);
    }

    @Override
    public int getByProductTypeCount(Integer productType, List<Integer> statusList) {
        if(Objects.isNull(productType)) {
            return 0;
        }
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getDeleteFlag, 0);
        wrapper.eq(Product::getProductType, productType);
        if(CollectionUtils.isNotEmpty(statusList)) {
            wrapper.in(Product::getProductStatus, statusList);
        }
        return this.count(wrapper);
    }

    @Override
    public List<Product> getProductList(ProductQueryVo queryVo) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getDeleteFlag, 0);
        if(CollectionUtils.isNotEmpty(queryVo.getProductTypeList())) {
            wrapper.in(Product::getProductType, queryVo.getProductTypeList());
        }
        if(Objects.nonNull(queryVo.getProductStatus())) {
            wrapper.eq(Product::getProductStatus, queryVo.getProductStatus());
        }
        return this.list(wrapper);
    }

    @Override
    public Product getReportProduct() {
        /*LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getDeleteFlag, 0);
        wrapper.eq(Product::getProductStatus, 0);
        wrapper.eq(Product::getProductType, ProductTypeEnum.PTYPE3.getType());
        wrapper.orderByDesc(Product::getId);
        wrapper.last(" limit 1");
        return this.getOne(wrapper, false);*/
        return getValidProductByType(ProductTypeEnum.PTYPE3.getType());
    }

    @Override
    public Product getValidProductByType(Integer productType) {
        if(Objects.isNull(productType)) {
            return null;
        }
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getDeleteFlag, 0);
        wrapper.eq(Product::getProductStatus, 0);
        wrapper.eq(Product::getProductType, productType);
        wrapper.orderByDesc(Product::getId);
        wrapper.last(" limit 1");
        return this.getOne(wrapper, false);
    }
}
